package com.woan.api.idao.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.transform.Transformers;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;


import com.woan.api.idao.SysAdmininfoMobileIDAO;
import com.woan.api.model.SysAdmininfo;
import com.woan.api.model.SysAdmininfoMobile;
import com.woan.api.vo.VmessageList;

/**
 * A data access object (DAO) providing persistence and search support for
 * SysAdmininfoMobile entities. Transaction control of the save(), update() and
 * delete() operations can directly support Spring container-managed
 * transactions or they can be augmented to handle user-managed Spring
 * transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 * 
 * @see com.woan.api.model.SysAdmininfoMobile
 * @author MyEclipse Persistence Tools
 */

public class SysAdmininfoMobileDAO extends HibernateDaoSupport  implements SysAdmininfoMobileIDAO  {
	public boolean save(SysAdmininfoMobile mobile)
	{
		try {
	
			getHibernateTemplate().saveOrUpdate(mobile);
			
			return true;
		} catch (RuntimeException re) {
			throw re;
		}
	}
	public SysAdmininfoMobile FindAdminMobileByObject(SysAdmininfoMobile mobile)
	{
		StringBuffer hql = new StringBuffer("from SysAdmininfoMobile");
		hql.append("  where uid = '"+mobile.getUid()+"' and deviceNum='"+mobile.getDeviceNum()+"' and deviceType='"+mobile.getDeviceType()+"' ");
		try
		{
			List<SysAdmininfoMobile> list=this.getHibernateTemplate().find(hql.toString());
			this.getHibernateTemplate().flush();
			this.getHibernateTemplate().clear();
			if(list!=null && list.size()>0)
			{
				return list.get(0);
			}
			else
				return null;
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		return null;
	}
	public SysAdmininfoMobile FindAdminPadByObject(SysAdmininfoMobile mobile)
	{
		StringBuffer hql = new StringBuffer("from SysAdmininfoMobile");
		hql.append("  where uid = '"+mobile.getUid()+"' and deviceType='"+mobile.getDeviceType()+"' ");
		try
		{
			List<SysAdmininfoMobile> list=this.getHibernateTemplate().find(hql.toString());
			this.getHibernateTemplate().flush();
			this.getHibernateTemplate().clear();
			if(list!=null && list.size()>0)
			{
				return list.get(0);
			}
			else
				return null;
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		return null;
	}
	public SysAdmininfoMobile FindAdminMobileByUserid(String userid)
	{
		StringBuffer hql = new StringBuffer("from SysAdmininfoMobile");
		hql.append("  where uid = '"+userid+"' ");
		try
		{
			List<SysAdmininfoMobile> list=this.getHibernateTemplate().find(hql.toString());
			
			if(list!=null && list.size()>0)
				return list.get(0);
			else
				return null;
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		return null;
	}
	public SysAdmininfoMobile FindAdminMobileByUseridAndDeviceNum(String userid, String devicenum)
	{
		StringBuffer hql = new StringBuffer("from SysAdmininfoMobile");
		hql.append("  where uid = '"+userid+"' and deviceNum = '"+devicenum+"' ");
		try
		{
			List<SysAdmininfoMobile> list=this.getHibernateTemplate().find(hql.toString());
			
			if(list!=null && list.size()>0)
				return list.get(0);
			else
				return null;
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		return null;
	}
	public SysAdmininfoMobile FindAdminMobileByGroupidAndDeviceNum(String groupid, String devicenum)
	{
		String sysAdim_hqlString="from SysAdmininfo where rid='"+groupid+"' ";
		
		List<SysAdmininfo> sysAdmins = this.getHibernateTemplate().find(sysAdim_hqlString);
		
		StringBuffer  buffer=new StringBuffer("from SysAdmininfoMobile  where deviceNum='"+devicenum+"' ");
		try
		{
			/*Query query = getSession().createSQLQuery(buffer.toString()).setResultTransformer(
					Transformers.aliasToBean(SysAdmininfoMobile.class));	*/	
			List<SysAdmininfoMobile> list=this.getHibernateTemplate().find(buffer.toString());
			for (SysAdmininfoMobile sysAdmininfoMobile : list) {
				for (SysAdmininfo sysAdmininfo : sysAdmins) {
					if (sysAdmininfoMobile.getUid().equals(sysAdmininfo.getUid())) {
						return sysAdmininfoMobile;
					}else {
						continue;
					}
				}
			}
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		return null;
	}
	
}